/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package procesos;

import entidades.Ejercicio;
import entidades.Nivel;
import java.beans.PropertyVetoException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Oswl_C
 */
public class MenuEjercicio extends javax.swing.JInternalFrame {

    /**
     * Creates new form MenuEjercicio
     */
    public MenuEjercicio() {
        initComponents();
        filtrarEjercicios();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("lecturadb?zeroDateTimeBehavior=convertToNullPU").createEntityManager();
        nivelQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT n FROM Nivel n");
        nivelList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : nivelQuery.getResultList();
        ejercicioQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT e FROM Ejercicio e");
        ejercicioList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(ejercicioQuery.getResultList());
        itemEjercicioQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT i FROM ItemEjercicio i");
        itemEjercicioList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : itemEjercicioQuery.getResultList();
        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Realizar Ejercicio");

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, nivelList, jComboBox1);
        bindingGroup.addBinding(jComboBoxBinding);

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        org.jdesktop.swingbinding.JListBinding jListBinding = org.jdesktop.swingbinding.SwingBindings.createJListBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, ejercicioList, jList1);
        bindingGroup.addBinding(jListBinding);

        jScrollPane1.setViewportView(jList1);

        jButton1.setText("Iniciar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap(573, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Ejercicio ejeObj = (Ejercicio) jList1.getSelectedValue();
        int codEjer = ejeObj.getEjeCodigo();
        if (codEjer >= 0) {
            try {
                RealizarEjercicio e = new RealizarEjercicio(ejeObj);
                LecturaMDI.desktopPane.add(e);
                e.setVisible(true);
                e.setMaximum(true);
            } catch (PropertyVetoException ex) {
                Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        filtrarEjercicios();
    }//GEN-LAST:event_jComboBox1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<entidades.Ejercicio> ejercicioList;
    private javax.persistence.Query ejercicioQuery;
    private javax.persistence.EntityManager entityManager;
    private java.util.List<entidades.ItemEjercicio> itemEjercicioList;
    private javax.persistence.Query itemEjercicioQuery;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private java.util.List<entidades.Nivel> nivelList;
    private javax.persistence.Query nivelQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private void filtrarEjercicios() {
        Nivel nivel = (Nivel) jComboBox1.getSelectedItem();
        if (nivel != null) {
            ejercicioQuery = entityManager.createQuery("SELECT e FROM Ejercicio e WHERE e.nivCodigo = :nivel").setParameter("nivel", nivel);
            ejercicioList = ejercicioQuery.getResultList();
            jList1.setListData(ejercicioList.toArray());
        }
    }
}
